package net.xo68.destiny.core.util;



import net.xo68.destiny.contract.common.SystemConstants;
import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

/**
 * base64编码工具类
 * @author wyy
 *@version 2015-11-25
 */
@SuppressWarnings("restriction")
public final class Base64Utils {


    public static  String encodeFromString(String srcString){
        return encodeFromString(srcString, SystemConstants.ENCODE_DEFAULT);
    }
    public static  String encodeFromString(String srcString, String charsetName){
        try {
            return encode(srcString.getBytes(charsetName),charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SystemConstants.STRING_EMPTY;
    }
    public static  String encode(byte[] srcBytes){
        return encode(srcBytes,SystemConstants.ENCODE_DEFAULT);
    }
    public static  String encode(byte[] srcBytes,String charsetName){
        try {
            return new String(Base64.encodeBase64(srcBytes),charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SystemConstants.STRING_EMPTY;
    }

    public static String decodeToString(String base64String){
        return decodeToString(base64String,SystemConstants.ENCODE_DEFAULT);
    }
    public static String decodeToString(String base64String,String charsetName){
        try {
            return new String(decode(base64String,charsetName),charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SystemConstants.STRING_EMPTY;
    }
    public static byte[] decode(String base64String){
        return decode(base64String,SystemConstants.ENCODE_DEFAULT);
    }
    public static byte[] decode(String base64String,String charsetName){
        try {
            return Base64.decodeBase64(base64String.getBytes(charsetName));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }





	public static void main(String... args){
	    String src="day day up!";

	    String enstr= encodeFromString(src);
        System.out.println(enstr);
        String destr=decodeToString(enstr);
        System.out.println(destr);
    }
}
